package com.hw.dagl.service.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hw.dagl.dao.impl.RoleDaoImpl;
import com.hw.dagl.model.Sys_Role;
import com.hw.dagl.service.RoleService;
import com.hw.dao.utils.EntityTools;
import com.hw.dao.utils.OraclePageDaoBase;
import com.hw.dao.utils.Page;
import com.hw.interceptor.SystemLog;

@Service
public class RoleServiceImpl implements RoleService{

	@Resource
	private RoleDaoImpl roleDaoImpl;
	@Resource
	private OraclePageDaoBase oraclePageDaoBase;
	
	/**
	 * 分页查询角色信息
	 */
	@Override
	@SystemLog(Modelname="角色管理",operationType="3",operationName="查询角色列表")
	public Page<Sys_Role> findRole(Page<Sys_Role> pages,Sys_Role info){
		StringBuffer sql=new StringBuffer(" select r.*,u.xm from SYS_ROLE r "
				+ " left join Sys_User u on r.cjr=u.yhdh "
				+ " where 1=1 ");
		List<Object> args=new ArrayList<Object>();
		if(StringUtils.isNotEmpty(info.getRid())){
			sql.append(" and ( r.rid like ? or r.rname like ? )");
			args.add("%"+info.getRid()+"%");
			args.add("%"+info.getRid()+"%");
		}
		sql.append(" order by r.rid asc ");
		return oraclePageDaoBase.queryForPage(sql.toString(), pages, new RowMapper<Sys_Role>(){
			private EntityTools<Sys_Role> et = new EntityTools<Sys_Role>();
			@Override
			public Sys_Role mapRow(ResultSet rs, int arg1)throws SQLException {
				Sys_Role role = new Sys_Role();
				et.setColumnsValueByResultSet(rs, role);
				return role;
			}
		}, args.toArray());
	}
	
	/**
	 * 根据角色ID查询用户信息
	 */
	@Override
	public Sys_Role findRoleByRid(String rid){
		String sql=" select r.*,u.xm from SYS_ROLE r "
				+ " left join Sys_User u on r.cjr=u.yhdh "
				+ " where 1=1 and r.rid ="+rid;
		List<Sys_Role> list =  oraclePageDaoBase.query(sql, new Object[]{}, new RowMapper<Sys_Role>(){
			private EntityTools<Sys_Role> et  = new EntityTools<Sys_Role>();
			@Override
			public Sys_Role mapRow(ResultSet rs,int arg1)throws SQLException {
				Sys_Role role = new Sys_Role();
				et.setColumnsValueByResultSet(rs, role);
				return role;
			}
		});
		return list.size() > 0? list.get(0) : null;
	}
	/*
	 * 查询�?��角色
	 */
	@Override
	public List<Sys_Role> findRoleAll(){
		String sql=" select r.* from SYS_ROLE r order by r.rid asc ";
		List<Sys_Role> list =  oraclePageDaoBase.query(sql, new Object[]{}, new RowMapper<Sys_Role>(){
			private EntityTools<Sys_Role> et  = new EntityTools<Sys_Role>();
			@Override
			public Sys_Role mapRow(ResultSet rs,int arg1)throws SQLException {
				Sys_Role role = new Sys_Role();
				et.setColumnsValueByResultSet(rs, role);
				return role;
			}
		});
		return list.size() > 0? list: null;
	}
	/**
	 * 批量删除角色信息
	 */
	@Override
	@SystemLog(Modelname="权限管理",operationType="4",operationName="删除角色")
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean delRole(String ids){
		if(StringUtils.isNotEmpty(ids)){
			ids = ids.replaceAll(",", "','");
			String sql = " delete from sys_role where rid in ('"+ids+"') ";
			String sql2 = " delete from SYS_ROLE_REF_MENU where rid in ('"+ids+"') ";
			int flag = oraclePageDaoBase.update(sql);
			oraclePageDaoBase.update(sql2);
			return flag != -1;
		}else{
			return true;
		}
	}
	/*
	 * 新增角色信息
	 */
	@Override
	@SystemLog(Modelname="角色管理",operationType="1",operationName="新增角色")
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean saveRole(Sys_Role info){
		if(StringUtils.isEmpty(info.getShqx())){
			info.setShqx("0");
		}
		String sql=" insert into sys_role (rid, rname, cjr, cjsj, ipks, ipjs, jsyxq,shqx) "
				+ " values (?, ?, ?, sysdate, ?, ?, ?,?) ";
		int flag=oraclePageDaoBase.update(sql, new Object[]{info.getRid(),info.getRname(),info.getCjr(),
				   info.getIpks(),info.getIpjs(),info.getJsyxq(),info.getShqx()});
//		roleDaoImpl.save(info);
		return flag!=-1;
	}
	
	/*
	 * 修改角色信息
	 */
	@Override
	@SystemLog(Modelname="角色管理",operationType="2",operationName="修改角色")
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean updateRole(Sys_Role info){
		String sql=" update sys_role set rname =?, ipks =?, ipjs =?, jsyxq =? where rid =? ";
		int flag=oraclePageDaoBase.update(sql, new Object[]{info.getRname(),info.getIpks(),info.getIpjs(),info.getJsyxq(),info.getRid()});
		return flag!=-1;
	}
	
	/*
	 * 保存角色目录信息
	 */
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public boolean saveRoleMenu(final String rid,final List<String> idList){
		boolean flag = false;
		oraclePageDaoBase.update(" DELETE FROM SYS_ROLE_REF_MENU WHERE rid = ? ", new Object[]{rid});
		
		int[] i = oraclePageDaoBase.batchUpdate("insert into SYS_ROLE_REF_MENU(rid,mid) values(?,?)", new BatchPreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps, int index) throws SQLException {
				ps.setString(1, rid);
				ps.setString(2, idList.get(index));
				
			}
			
			@Override
			public int getBatchSize() {
				// TODO Auto-generated method stub
				return idList.size();
			}
		});
		if(i.length>0){
			flag= true;
		}
		return flag;
	}
	/**
	 * 生成角色ID
	 */
	@Override
	public int findRoleRid(){
		String sql = " select sys_role_rid.nextval from dual ";
		int rid = oraclePageDaoBase.queryForInt(sql);
		return rid;
	}
}
